package com.tejava.cafe_in;

import java.net.ContentHandler;
import java.util.ArrayList;

import com.tejava.DB.DbHelper;
import com.tejava.DB.History;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabHost;


public class Tab03 extends ListActivity {
    private ArrayList<History> list;
    private HistoryAdapter adapter;
    History tempHistory;
    DbHelper db;
    boolean loaded = false;
    
    private TabHost mTabHost;
    private View layout;
    
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.tab03);
		loaded = false;
		list = new ArrayList<History>();
		db = new DbHelper(this);
		
		//tempHistory.setName("되냐안되냐");
		//tempHistory.setDate("2011/11/22");
		//list.add(tempHistory);
		//list.add("�⑤벊占쏙옙�占�);
		//list.add("占싼딉옙占쏙옙占쏙옙占�占썬끉占�);
		//list.add("占쎈똻占쏙옙占쏙옙");
		//insertDB();
		
		LayoutInflater inflater = (LayoutInflater)getLayoutInflater().getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = inflater.inflate(R.layout.tab03, null);
		
		setContentView(layout);
		
		viewDB();
		
		// listView 수정
		 adapter = new HistoryAdapter(this, 
	        		R.layout.list_layout_text, list);

		 ((ListView)layout.findViewById(android.R.id.list)).setAdapter(adapter);
//		 setListAdapter(adapter);
	     getListView().setBackgroundColor(Color.WHITE);
	     getListView().setCacheColorHint(0);		
	     
	}
	
	public void insertDB(){
		
		History emp=new History("HeukSeon","","323-1", "02-820-5309", "123123123", "12333444555");
		History emp2=new History("Jeongja","","333-1", "02-820-5311", "123123123", "12333444555");
		
		db.AddHistory(emp);
		db.AddHistory(emp2);

	}
	
	public void viewDB(){
		Cursor mCursor = db.getAllHistorys();
		
		while(mCursor.moveToNext()){
			tempHistory = new History(mCursor.getString(1),mCursor.getString(2));
			
			list.add(tempHistory);
		}
		
		if(mCursor.moveToFirst()==true){
			layout.findViewById(R.id.historyView).setVisibility(View.GONE);
		}
		else{
			layout.findViewById(R.id.historyView).setVisibility(View.VISIBLE);
		}
		
	}
	
	public void removeAllDB(){
		db.RemoveAllHistory();
	}
	
	 @Override
	    protected void onListItemClick (ListView l, View v, int position, long id){
	    	super.onListItemClick(l, v, position, id);
	    	//getLocal activity manager 
	    	
	    	//mTabHost = (TabHost)findViewById(android.R.id.tabhost);
	    	//mTabHost.setup();
	    	CustomTabActivity a = (CustomTabActivity)getParent();
	    	TabHost mTabHost = a.mTabHost;
	    	
	    	Intent sendIntent = new Intent("com.tejava.cafe_in.HistoryClicked");
	    	sendIntent.putExtra("HistoryName", list.get(position).getName());
	    	sendBroadcast(sendIntent);
	    	
	    	mTabHost.setCurrentTab(1);
	    	
	    	
	    	Log.v("tab3", "helloWorld");
	    	//mTabHost.setCurrentTab(1);
	    	//mTabHost.getCurrentTab();
//	    	
//	    	if(position == 0){
//	    		Intent i = new Intent(this, Tab04_Notice.class);
//	    		startActivity(i);
////				finish();
//	    	}
//	    	else if(position == 1){
//	    		Intent i = new Intent(this, Tab04_Setup.class);
//				startActivity(i);
////				finish();
//	    	}
//	    	else if(position == 2){
//	    		Intent i = new Intent(this, Tab04_Guide.class);
//				startActivity(i);
////				finish();
//	    	}
	    }
	 
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) {
	 	getMenuInflater().inflate(R.menu.tab03_menu, menu);
	 	return true;
	 }
	 
	 @Override
	 public boolean onMenuItemSelected(int featureId, MenuItem item) {
	 	switch (item.getItemId()) {
	 		case R.id.listDeletionIcon:
	 			db.RemoveAllHistory();
	 			list.clear();
	 			adapter.notifyDataSetChanged();
	 			break;
	 	}
	 	return true;
	 }
	
	 @Override
	 public void onResume(){
		 super.onResume();
	        if (!loaded) {
	            //First time just set the loaded flag true
	            loaded = true;
	        } else {
	            Log.i("Resuming", "back to my first activity");
	            
	            //Reload data
	            list.clear();
	            viewDB();
	           
	            adapter.notifyDataSetChanged();
	            adapter.setNotifyOnChange (true);
	        }
	 }

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			new AlertDialog.Builder(Tab03.this)
					.setTitle(getResources().getString(R.string.app_name))
					.setMessage("프로그램을 종료하시겠습니까?")
					.setPositiveButton("네",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									moveTaskToBack(true);
									finish();
								}
							}).setNegativeButton("아니오", null).show();
		}
		return true;
	}
	
}
